﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// Add custom library
using SudokuSolver.Classes.Core;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI;

namespace SudokuSolver
{
    public sealed partial class MainPage : Page
    {
        public Sudoku sudoku;

        private Cell tappedCell;

        public MainPage()
        {
            InitializeComponent();
            InitializeBackground();

            // Initialize Sudoku
            sudoku = new Sudoku();
            tappedCell = null;
            //sudoku.LoadFromString("......9.7...42.18....7.5.261..9.4....5.....4....5.7..992.1.8....34.59...5.7......");
            sudoku.LoadFromString(".......1.3..61.5474....5..95......6...69.83...9....8.19..8....5875.32..6.3........");
            sudokuGrid.ItemsSource = sudoku.GetData();
        }

        private void InitializeBackground()
        {
            SudokuPanel.Background = new ImageBrush
            {
                Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill,
                ImageSource = new BitmapImage { UriSource = new Uri("ms-appx:///Assets/Background3.jpg") }
            };

            sudokuGrid.Background = new ImageBrush
            {
                Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill,
                ImageSource = new BitmapImage { UriSource = new Uri("ms-appx:///Assets/GridBackground2.png") }
            };
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void PopUpSelectionFocus(object sender, RoutedEventArgs e)
        {
            TextBlock tappedTextBlock = (TextBlock) sender;
            tappedCell = (Cell) tappedTextBlock.DataContext;

            checkPossibility(tappedCell);            
                
            if (!tappedCell.WasPresent)
            {
                selectionPopUp.Margin = GetMarginNumPad(tappedCell);
                selectionPopUp.IsOpen = !selectionPopUp.IsOpen;
            }
        }

        private void checkPossibility(Cell cell)
        {
            List<NumPadCase> listeNumPadCase = new List<NumPadCase>();
            for (int i = 1; i < 10; i++)
            {
                NumPadCase numPadCase = new NumPadCase();
                numPadCase.Value = i;
                numPadCase.IsEnabled = sudoku.IsConsistentPad(i, cell.RowIndex - 1, cell.ColumnIndex - 1) || !EasyMode.IsOn;
                numPadCase.Foreground = numPadCase.IsEnabled || !EasyMode.IsOn ? new SolidColorBrush(Colors.White) : new SolidColorBrush(Colors.Red);
                listeNumPadCase.Add(numPadCase);
            }
            selectionGrid.ItemsSource = listeNumPadCase;
        }

        private void PopUpSelectionLostFocus(object sender, RoutedEventArgs e)
        {
            selectionPopUp.IsOpen = false;
        } 

        private Thickness GetMarginNumPad(Cell tppedCell)
        {
            int marginLeft = 0;
            int marginRight = 0;
            int marginTop = 0;
            int marginBottom = 0;

            if (tappedCell.ColumnIndex >= 1 && tappedCell.ColumnIndex < 4)
            {
                marginRight = 400;
            }

            if (tappedCell.ColumnIndex > 6)
            {
                marginLeft = 400;
            }

            if (tappedCell.RowIndex >= 1 && tappedCell.RowIndex < 4)
            {
                marginBottom = 400;
            }

            if (tappedCell.RowIndex > 6)
            {
                marginTop = 400;
            }

            return new Thickness(marginLeft, marginTop, marginRight, marginBottom);
        }

        private void NumPadTapped(object sender, RoutedEventArgs e)
        {
            TextBlock tappedTextBlock = (TextBlock) sender;
            int value = int.Parse(tappedTextBlock.Text);

            tappedCell.Value = value;
            sudokuGrid.ItemsSource = sudoku.GetData();
            selectionPopUp.IsOpen = !selectionPopUp.IsOpen;
        }

        private void SwitchToHandCursor(object sender, RoutedEventArgs e)
        {
            Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Hand, 1);
        }

        private void SwitchToDefaultCursor(object sender, RoutedEventArgs e)
        {
            Window.Current.CoreWindow.PointerCursor = new Windows.UI.Core.CoreCursor(Windows.UI.Core.CoreCursorType.Arrow, 1);
        }

        #region Button Handlers
        private void BtnSolveClick(object sender, RoutedEventArgs e)
        {
            sudoku.Solve();
            sudokuGrid.ItemsSource = sudoku.GetData();
        }

        private void BtnImportPictureClick(object sender, RoutedEventArgs e)
        {

        }

        private void BtnTakePictureClick(object sender, RoutedEventArgs e)
        {

        }

        private void BtnBlackBoardStyleClick(object sender, RoutedEventArgs e)
        {
            InitializeBackground();
        }

        private void BtnSheetStyleClick(object sender, RoutedEventArgs e)
        {
            SudokuPanel.Background = new ImageBrush
            {
                Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill,
                ImageSource = new BitmapImage { UriSource = new Uri("ms-appx:///Assets/Background_sheet.jpg") }
            };

            sudokuGrid.Background = new ImageBrush
            {
                Stretch = Windows.UI.Xaml.Media.Stretch.UniformToFill,
                ImageSource = new BitmapImage { UriSource = new Uri("ms-appx:///Assets/GridBackgroundBlack2.png") }
            };
        }

        private void BtnSettingsClick(object sender, RoutedEventArgs e)
        {

        }

        private void BtnExitClick(object sender, RoutedEventArgs e)
        {
            Application.Current.Exit();
        }
        #endregion
    }
}
